package com.lyj.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.lyj.exception.BaseException;
import com.lyj.mapper.FamilyMapper;
import com.lyj.mapper.FamilyMemberMapper;
import com.lyj.mapper.UserMapper;
import com.lyj.pojo.Family;
import com.lyj.pojo.FamilyMember;
import com.lyj.pojo.User;
import com.lyj.service.PersonalService;
import com.lyj.pojo.vo.FamilyMemberVO;
import com.lyj.pojo.vo.UserInfoVO;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import java.util.List;
import java.util.stream.Collectors;

@Service
public class PersonalServiceImpl implements PersonalService {

    @Autowired
    private UserMapper userMapper;

    @Autowired
    private FamilyMapper familyMapper;

    @Autowired
    private FamilyMemberMapper familyMemberMapper;

    @Override
    public UserInfoVO getUserInfoForView(Integer userId) {
        // 查询用户基本信息
        User user = userMapper.selectById(userId);
        if (user == null) {
            throw new BaseException("用户不存在");
        }

        // 查询用户在家庭中的关系
        LambdaQueryWrapper<FamilyMember> memberQuery = new LambdaQueryWrapper<>();
        memberQuery.eq(FamilyMember::getUserId, userId);
        FamilyMember familyMember = familyMemberMapper.selectOne(memberQuery);

        if (familyMember == null) {
            throw new BaseException("用户未加入任何家庭");
        }

        // 查询家庭信息
        Family family = familyMapper.selectById(familyMember.getFamilyId());

        // 组装 VO
        UserInfoVO userInfoVO = new UserInfoVO();
        BeanUtils.copyProperties(user, userInfoVO);
        userInfoVO.setFamilyName(family.getFamilyName());
        userInfoVO.setRole(familyMember.getRole());
        userInfoVO.setFamilyId(familyMember.getFamilyId());

        return userInfoVO;
    }

    @Override
    public List<FamilyMemberVO> getFamilyMembersForView(Integer userId) {
        // 先查询当前用户在家庭中的角色，判断是否为管理员
        LambdaQueryWrapper<FamilyMember> currentMemberQuery = new LambdaQueryWrapper<>();
        currentMemberQuery.eq(FamilyMember::getUserId, userId);
        FamilyMember currentMember = familyMemberMapper.selectOne(currentMemberQuery);

        if (currentMember == null) {
            throw new BaseException("用户未加入任何家庭");
        }

        if (!"管理员".equals(currentMember.getRole())) {
            throw new BaseException("无权限查看家庭成员列表");
        }

        // 查询该家庭的所有成员
        LambdaQueryWrapper<FamilyMember> membersQuery = new LambdaQueryWrapper<>();
        membersQuery.eq(FamilyMember::getFamilyId, currentMember.getFamilyId());
        List<FamilyMember> members = familyMemberMapper.selectList(membersQuery);

        // 转换为 VO 列表并补充用户名
        return members.stream().map(member -> {
            FamilyMemberVO vo = new FamilyMemberVO();
            BeanUtils.copyProperties(member, vo);
            User user = userMapper.selectById(member.getUserId());
            vo.setUsername(user.getUsername());
            return vo;
        }).collect(Collectors.toList());
    }
}